<?php


namespace app\common\model;

use think\Model;
use think\Db;
class MobileCode extends Model
{
    protected $table = "store_mobile_code";

    public function code($params){
        $code = rand(1000,9999);

        if(!empty($params['mobile'])){
            $mobile = $params['mobile'];

            $host = "https://gyytz.market.alicloudapi.com";
            $path = "/sms/smsSend";
            $method = "POST";
            $appcode = "88e04f763ca740108ca92d405dd92b98";
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appcode);

//smsSignId（短信前缀）和templateId（短信模板），可登录国阳云控制台自助申请。参考文档：http://help.guoyangyun.com/Problem/Qm.html

            $querys = "mobile=$mobile&param=**code**%3A$code%2C**minute**%3A5&smsSignId=2e65b1bb3d054466b82f0c9d125465e2&templateId=908e94ccf08b4476ba6c876d13f084ad";
            $bodys = "";
            $url = $host . $path . "?" . $querys;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, true);  //如果只想获取返回参数，可设置为false
            if (1 == strpos("$".$host, "https://"))
            {
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
            curl_exec($curl);

            self::insert(['mobile'=>$params['mobile'],'code'=>$code,'add_time'=>date('Y-m-d H:i:s')]);
            return $code;
        }
    }
}